package com.fw.payment.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fw.payment.bao.MerchantInterfaceBAO;
import com.fw.payment.bao.PayAtAllInterfaceBAO;
import com.fw.payment.bao.ServiceProfileBAO;
import com.fw.payment.util.RequestUtil;
import com.fw.service.inboundmessagelog.bean.InboundMessageLogBean;
import com.fw.service.outboundmessagelog.bean.OutboundMessageLogBean;
import com.fw.service.serviceprofile.bean.ServiceProfileBean;

@Controller
@RequestMapping(value = "/")
public class PaymentController extends ReponseBackgroundController {

    private static final Logger log = Logger.getLogger(PaymentController.class);

    public static final String ACTION_CASH = "/payment/cash";

    // @Autowired
    // private MerchantProfileBAO merchantProfileBAO;

    @Autowired
    private ServiceProfileBAO serviceProfileBAO;

    @Autowired
    private MerchantInterfaceBAO merchantInterfaceBAO;

    @Autowired
    private PayAtAllInterfaceBAO payAtAllInterfaceBAO;

    @RequestMapping(value = PaymentController.ACTION_CASH, method = RequestMethod.POST)
    public String cash(@Valid @ModelAttribute("inboundMessageLogBean") InboundMessageLogBean inboundMessageLogBean, BindingResult result,
	    ModelMap model, HttpServletRequest request, Locale locale) {
	ServiceProfileBean serviceProfileBean = null;
	try {
	    Boolean success = merchantInterfaceBAO.receiveInboundMessageLog(inboundMessageLogBean);
	    String serviceId = inboundMessageLogBean.getServiceID();
	    if (serviceId != null) {
		serviceProfileBean = serviceProfileBAO.getServiceProfileByServiceId(serviceId);
	    }
	    if (Boolean.FALSE.equals(success)) {
		return returnView("error", inboundMessageLogBean, serviceProfileBean);
	    }
	    OutboundMessageLogBean outboundMessageLogBean = payAtAllInterfaceBAO.createOutboundMessageLog(inboundMessageLogBean,
		    serviceProfileBean);
	    if (outboundMessageLogBean == null || outboundMessageLogBean.getId() == null) {
		return returnView("error", inboundMessageLogBean, serviceProfileBean);
	    }
	    String getURLWithContextPath = RequestUtil.getURLWithContextPath(request);
	    request.setAttribute("payAtAllAction", getURLWithContextPath + "/simulate/payatall_paynow");
	    request.setAttribute("outboundMessageLogBean", outboundMessageLogBean);
	} catch (Exception e) {
	    log.error(e, e);
	    return returnView("error", inboundMessageLogBean, serviceProfileBean);
	}
	return returnView("payment/cash", inboundMessageLogBean, serviceProfileBean);
    }

}
